package com.szewec.ps.ui.road.facilities.investigation.task.roadside.facilities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.base.Enums;
import com.szewec.ps.databinding.FragmentRoadParkingBinding;
import com.szewec.ps.model.road.facilities.investigation.road.side.facilities.RoadParking;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTask;
import com.szewec.ps.model.road.facilities.investigation.task.RoadFacilitiesInvestigationTaskDTO;
import com.szewec.ps.model.selectmappoint.SelectedPoint;
import com.szewec.ps.myview.pickerview.TimePickerView;
import com.szewec.ps.ui.road.facilities.intersection.task.views.RadioBoxView;
import com.szewec.ps.util.DateUtil;
import com.szewec.ps.util.RollUtil;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.TextViewUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 路内停车
 */
public class RoadParkingFragment extends Fragment {
    private FragmentRoadParkingBinding binding;

    // 停放车种 数据字典
    private List<String> parkingTypeDict = new ArrayList<>();
    // 停放位置 数据字典
    private List<String> parkingPositionDict = new ArrayList<>();
    // 停放方式 数据字典
    private List<String> parkingModeDict = new ArrayList<>();
    // 停车标志 数据字典
    private List<String> stopSignDict = new ArrayList<>();

    // 路段设施调查任务DTO
    private RoadFacilitiesInvestigationTaskDTO.RoadFacilitiesTaskDTO roadFacilitiesTaskDTO;
    private RoadParking roadParking;

    // 坐标相关
    private static final int START_POINT_INT_FLAG = 1;
    private static final int END_POINT_INT_FLAG = 2;
    private SelectedPoint centerPoint;
    private SelectedPoint startSelectPoint;
    private SelectedPoint endSelectPoint;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_road_parking, container, false);
        // 2、获取到视图
        View inflate = binding.getRoot();
        ButterKnife.bind(this, inflate);

        initData();
        initView();
        return inflate;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        // 通过Activity中的public方法来获取值
        roadFacilitiesTaskDTO = ((RoadsideFacilitiesActivity) context).pushFragmentData();
    }

    private void initData() {
        parkingTypeDict = Enums.ParkingTypeEnum.getEnumValueList();
        parkingPositionDict = Enums.ParkingPositionTypeEnum.getEnumValueList();
        parkingModeDict = Enums.ParkingModeTypeEnum.getEnumValueList();
        stopSignDict = Enums.StopSignEnum.getEnumValueList();
        roadParking = roadFacilitiesTaskDTO.getRoadParking() == null ? new RoadParking() : roadFacilitiesTaskDTO.getRoadParking();
    }

    private void initView() {
        // 给坐标按钮绑定Code
        binding.startingPoint.setRequestCode(START_POINT_INT_FLAG);
        binding.endPoint.setRequestCode(END_POINT_INT_FLAG);

        // 获取坐标
        SelectedPoint startPoint = JSONObject.parseObject(roadParking.getStartingPoint(), SelectedPoint.class);
        startPoint = startPoint == null ? new SelectedPoint() : startPoint;
        SelectedPoint endPoint = JSONObject.parseObject(roadParking.getEndPoint(), SelectedPoint.class);
        endPoint = endPoint == null ? new SelectedPoint() : endPoint;
        // 设置类型
        Integer setType = roadParking.getSetType() == null ? RadioBoxView.ONE : roadParking.getSetType();
        // 管理方式
        Integer managementStyle = roadParking.getManagementStyle() == null ? RadioBoxView.ONE : roadParking.getManagementStyle();
        // 收费情况
        Integer chargeSituation = roadParking.getChargeSituation() == null ? RadioBoxView.ONE : roadParking.getChargeSituation();

        binding.roadName.setValue(roadParking.getRoadName());
        binding.startingPoint.setValue(startPoint.getxCoordinate(), startPoint.getyCoordinate());
        binding.endPoint.setValue(endPoint.getxCoordinate(), endPoint.getyCoordinate());
        binding.parkingType.setValue(Enums.ParkingTypeEnum.getValueByKey(roadParking.getParkingType()));
        binding.parkingPosition.setValue(Enums.ParkingPositionTypeEnum.getValueByKey(roadParking.getParkingPosition()));
        binding.parkingSpaceNumber.setValue(roadParking.getParkingSpaceNumber());
        binding.freeParkingSpace.setValue(roadParking.getFreeParkingSpace());
        binding.setType.setChecked(setType);
        binding.parkingMode.setValue(Enums.ParkingModeTypeEnum.getValueByKey(roadParking.getParkingMode()));
        binding.openingTime.getSelectTextView().setText(DateUtil.convertDateToString(roadParking.getOpeningTime()));
        binding.closingTime.getSelectTextView().setText(DateUtil.convertDateToString(roadParking.getClosingTime()));
        binding.managementStyle.setChecked(managementStyle);
        binding.stopSign.setValue(Enums.StopSignEnum.getValueByKey(roadParking.getStopSign()));
        binding.chargeSituation.setChecked(chargeSituation);
    }

    public RoadParking getData() {
        // 添加路段调查任务ID
        RoadFacilitiesInvestigationTask facilitiesInvestigationTask = roadFacilitiesTaskDTO.getFacilitiesInvestigationTask();
        if (facilitiesInvestigationTask != null && StringUtils.isNotEmpty(facilitiesInvestigationTask.getId())) {
            roadParking.setRoadInvestigationTaskId(facilitiesInvestigationTask.getId());
        }
        // 坐标
        String startPointStr = startSelectPoint == null ? null : JSONObject.toJSONString(startSelectPoint);
        String endPointStr = endSelectPoint == null ? null : JSONObject.toJSONString(endSelectPoint);

        roadParking.setRoadName(binding.roadName.getStringValue());
        roadParking.setStartingPoint(startPointStr);
        roadParking.setEndPoint(endPointStr);
        roadParking.setParkingType(Enums.ParkingTypeEnum.getKeyByValue(binding.parkingType.getStringValue()));
        roadParking.setParkingPosition(Enums.ParkingPositionTypeEnum.getKeyByValue(binding.parkingPosition.getStringValue()));
        roadParking.setParkingSpaceNumber(binding.parkingSpaceNumber.getIntValue());
        roadParking.setFreeParkingSpace(binding.freeParkingSpace.getIntValue());
        roadParking.setSetType(binding.setType.getChecked());
        roadParking.setParkingMode(Enums.ParkingModeTypeEnum.getKeyByValue(binding.parkingMode.getStringValue()));
        roadParking.setOpeningTime(DateUtil.convertStringToDate(binding.openingTime.getStringValue()));
        roadParking.setClosingTime(DateUtil.convertStringToDate(binding.closingTime.getStringValue()));
        roadParking.setManagementStyle(binding.managementStyle.getChecked());
        roadParking.setStopSign(Enums.StopSignEnum.getKeyByValue(binding.stopSign.getStringValue()));
        roadParking.setChargeSituation(binding.chargeSituation.getChecked());

        return roadParking;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //选择了坐标
        if (resultCode == Constants.RESULT_CODE.SELECTED_POINT) {
            if (data != null) {
                Bundle presentValue = data.getExtras();
                centerPoint = (SelectedPoint) presentValue.getSerializable(Constants.EXTRA_KEY.MAP_CENTER_POINT);
            }
        }
        if (centerPoint == null) {
            return;
        }
        // 将获取到的坐标赋给相对应的对象
        switch (requestCode) {
            // 起点
            case START_POINT_INT_FLAG:
                startSelectPoint = centerPoint;
                break;
            // 终点
            case END_POINT_INT_FLAG:
                endSelectPoint = centerPoint;
                break;
        }
    }

    @OnClick({R.id.parkingType, R.id.parkingPosition, R.id.parkingMode, R.id.stopSign, R.id.openingTime, R.id.closingTime})
    public void onViewClicked(View view) {
        //隐藏系统软键盘
        TextViewUtil.closeKeyboard(getActivity());
        switch (view.getId()) {
            case R.id.parkingType:
                RollUtil.alertBottomWheelOption(getContext(), parkingTypeDict, binding.parkingType.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.parkingType.setValue(parkingTypeDict.get(index));
                    }
                });
                break;
            case R.id.parkingPosition:
                RollUtil.alertBottomWheelOption(getContext(), parkingPositionDict, binding.parkingPosition.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.parkingPosition.setValue(parkingPositionDict.get(index));
                    }
                });
                break;
            case R.id.parkingMode:
                RollUtil.alertBottomWheelOption(getContext(), parkingModeDict, binding.parkingMode.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.parkingMode.setValue(parkingModeDict.get(index));
                    }
                });
                break;
            case R.id.stopSign:
                RollUtil.alertBottomWheelOption(getContext(), stopSignDict, binding.stopSign.getSelectTextView(), new RollUtil.OnWheelViewClick() {
                    @Override
                    public void onClick(View view, int index) {
                        binding.stopSign.setValue(stopSignDict.get(index));
                    }
                });
                break;
            case R.id.openingTime:
                RollUtil.alertTimerPicker(binding.openingTime.getSelectTextView(), TimePickerView.Type.ALL, "yyyy-MM-dd HH:mm:ss", new RollUtil.TimerPickerCallBack() {
                    @Override
                    public void onTimeSelect(String date) {
                        binding.openingTime.getSelectTextView().setText(date);
                    }
                });
                break;
            case R.id.closingTime:
                RollUtil.alertTimerPicker(binding.closingTime.getSelectTextView(), TimePickerView.Type.ALL, "yyyy-MM-dd HH:mm:ss", new RollUtil.TimerPickerCallBack() {
                    @Override
                    public void onTimeSelect(String date) {
                        binding.closingTime.getSelectTextView().setText(date);
                    }
                });
                break;
        }
    }
}